/**
 *  *2010-4-2 上午10:52:30  add 
 */
package com.base.myproject.client.busi.service.gcode;

import com.base.myproject.client.system.BaseResource;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Image;

/**
 * @author Administrator
 *2010-4-2
 * javazgw@gmail.com
 */
public class OneGcode extends ContentPanel{
	String[] Url;
	Image image = new Image();
	
	Image pre =new Image();
	Image next =new Image();
	int curindex;
	Html html = new Html();
	
	public OneGcode(String[] Url,String html)
	{
		this.Url = Url;
		this.html = new Html(html);
		init();
	}
	private void init() {
		initwidget();
		
	}
	private void initwidget() {
		
		
		pre.setResource(BaseResource.INSTANCE.pre_dark());
		next.setResource(BaseResource.INSTANCE.next_dark());
		VerticalPanel v = new VerticalPanel();
		v.setHorizontalAlign(HorizontalAlignment.CENTER);
		v.setVerticalAlign(VerticalAlignment.MIDDLE);
		
		curindex=0;
		image.setSize("320", "320");
		if(Url!=null && Url.length>0)
			image.setUrl(Url[curindex]);
		
		
		pre.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				
				if(curindex>0)
					image.setUrl(Url[--curindex]);
				else
				{
					curindex = Url.length-1;
					image.setUrl(Url[curindex]);
				}
			}
			
		});
		pre.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				
				pre.setResource(BaseResource.INSTANCE.pre_dark());
			}
			
		});
		pre.addMouseOverHandler(new MouseOverHandler(){

			@Override
			public void onMouseOver(MouseOverEvent event) {
				
				pre.setResource(BaseResource.INSTANCE.pre_light());
			}
			
		});
		
		next.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				
				next.setResource(BaseResource.INSTANCE.next_dark());
			}
			
		});
		next.addMouseOverHandler(new MouseOverHandler(){

			@Override
			public void onMouseOver(MouseOverEvent event) {
				
				next.setResource(BaseResource.INSTANCE.next_light());
			}
			
		});
		
		next.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
		
				
				if(curindex<Url.length-1)
					image.setUrl(Url[++curindex]);
				else
				{
					curindex = 0;
					image.setUrl(Url[curindex]);
				}
				
			}
			
		});
//		pre.addListener(Events.Select, getListener());
//		next.addListener(Events.Select, getListener());
		
		
		
		
		HorizontalPanel h3 = new HorizontalPanel();
		h3.setHorizontalAlign(HorizontalAlignment.CENTER);
		h3.setVerticalAlign(VerticalAlignment.TOP);
		h3.add(pre);
		h3.add(next);
		
		HorizontalPanel h2 = new HorizontalPanel();
		h2.setHorizontalAlign(HorizontalAlignment.CENTER);
		h2.setVerticalAlign(VerticalAlignment.TOP);
		
		v.add(image);
		v.add(h3);
		h2.add(v);
		h2.add(html);
		h2.setSpacing(5);
		this.setLayout(new FitLayout());
		this.setHeaderVisible(false);
		this.add(h2);
	}
	
/*	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals(pre)) {   
								if(curindex<Url.length)
								image.setUrl(Url[curindex++]);
								else
								{
									curindex=Url.length-1;
									image.setUrl(Url[curindex]);
								}
								 
                                                                     
							}       
							else if (be.getSource().equals(next)) {   
								
								if(curindex>0)
									image.setUrl(Url[curindex--]);
								else
								{
									curindex=Url.length-1;
									image.setUrl(Url[curindex]);
								}
                                
							}  
						}
						else if (be.getType() == Events.Hide) {    
							
						}
						else if (be.getType() == Events.Change) {              
							if (be.getSource().equals("")) {         
                                                                     
							}                                                      
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}

		                                                       
                                                                     
		};                                                               
		return defaultlistener;                                          
	}*/
	public String[] getUrl() {
		return Url;
	}
	public void setUrl(String[] url) {
		Url = url;
		if(Url.length>0)
			image.setUrl(Url[curindex]);
	}   

	
}
